/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.app.command.query;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itgrail.pilot.app.command.cmo.query.PageQueryMetadataCmd;
import com.itgrail.pilot.app.command.co.DictDataCO;
import com.itgrail.pilot.app.command.co.DictTypeListCO;
import com.itgrail.pilot.domain.user.contant.OkrConstants;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.DictDataMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.DictTypeMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.DictDataDO;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.DictTypeDO;
import org.springframework.beans.factory.annotation.Autowired;
import com.itgrail.grail.cqrs.annotation.CmdHandler;
import com.itgrail.grail.cqrs.command.CommandExecutorI;
import com.itgrail.grail.dto.PageResult;
import com.itgrail.grail.dto.ResultData;
import com.itgrail.grail.utils.reflect.BeanMapper;

import java.util.List;


/**
 * 分页数据字典对应的命令执行器
 */
@CmdHandler
public class PageQueryMetadataCmdExe implements CommandExecutorI<ResultData<PageResult<DictTypeListCO>>, PageQueryMetadataCmd> {

    @Autowired
    private DictTypeMapper dictTypeMapper;

    @Autowired
    private DictDataMapper dictDataMapper;

    @Override
    public ResultData<PageResult<DictTypeListCO>> execute(PageQueryMetadataCmd cmd) {
        Page pageRequest = new Page(cmd.getPageNo(), cmd.getPageSize());
        QueryWrapper<DictTypeDO> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(cmd.getSearchKey())) {
            queryWrapper.and(qw -> qw
                    .like("dict_code", cmd.getSearchKey()).or()
                    .like("dict_name", cmd.getSearchKey()));
        }
        queryWrapper.eq("is_deleted", OkrConstants.NOT_DELETED);
        queryWrapper.orderByAsc("id");
        IPage<DictTypeDO> page = dictTypeMapper.selectPage(pageRequest, queryWrapper);
        List<DictTypeListCO> list = BeanMapper.mapList(page.getRecords(), DictTypeDO.class, DictTypeListCO.class);
        for (DictTypeListCO dictTypeListCO : list) {
            List<DictDataDO> dictDataList = dictDataMapper.findDictDataByDictCode(dictTypeListCO.getDictCode());
            dictTypeListCO.setDictDataModelList(BeanMapper.mapList(dictDataList, DictDataDO.class, DictDataCO.class));
        }
        PageResult<DictTypeListCO> pageResult = new PageResult<DictTypeListCO>();
        pageResult.setCurrentPage(page.getCurrent());
        pageResult.setTotalCount(page.getTotal());
        pageResult.setList(list);
        pageResult.setTotalPage(page.getSize());
        return ResultData.success(pageResult);

    }

}
